<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Order_confirm extends CI_Controller{
	function __construct(){
	
		parent::__construct();
		if(!$this->session->userdata('user_id') && !$this->session->userdata('user_display_name') ):
		redirect('backend');
		endif;
		$this->load->helper('form');
		//$this->output->enable_profiler(TRUE);
	}

	function index(){
		$offset = $this->uri->segment(4);
		if($offset==''):
			$offset = 0;
		endif;
		$this->load->library('pagination');
		$config['base_url'] = base_url().'/backend/order_confirm/index';
		$config['total_rows'] = count($this->get_all());
		$config['per_page'] = $this->config->item('per_page');
		$config['uri_segment'] = '4';
		
		$data['title'] = 'Konfirmasi Order';
		$data['template'] = 'order/confirmorder_index';
		$data['content'] = $this->get_all();
		$this->pagination->initialize($config);
		$this->load->view('backend/index',$data);
	}
	
	function view($konfirmasi_id=FALSE)
	{
		if($konfirmasi_id):
			$this->form_validation->set_rules('status', 'Status','required');
			if ($this->form_validation->run() == FALSE):
				$data['title'] = 'Detail Konfirmasi Order';
				$data['template'] = 'order/confirmorder_view';
				$data['row'] = $this->get_data($konfirmasi_id);
				$this->load->view('backend/index',$data);
			else:
				$data = array('status'=>$this->input->post('status'));
				$this->db->where('konfirmasi_id',$this->input->post('konfirmasi_id'))
				->update('tb_konfirmasi',$data);
				redirect('backend/order_confirm');
			endif;
		else:
			redirect('backend/order_confirm');
		endif;
	}
	
	function ajax_bulk_action($param){
		foreach($_POST["konfirmasi_id"] as $konfirmasi_id):
			if($param=='approve'):
				$data = array('status'=>'approved');
				$this->db->where('konfirmasi_id',$konfirmasi_id)->update('tb_konfirmasi',$data);
			elseif($param=='unapprove'):
				$data = array('status'=>'not approved');
				$this->db->where('konfirmasi_id',$konfirmasi_id)->update('tb_konfirmasi',$data);
			elseif($param=='delete'):
				$this->db->where('konfirmasi_id',$konfirmasi_id)->delete('tb_konfirmasi');
			endif;
		endforeach;
	}
	
	private function get_data($konfirmasi_id)
	{
		return $this->db->where('konfirmasi_id',$konfirmasi_id)
				->get('tb_konfirmasi')->row();
	}
	
	private function get_all($limit=FALSE,$offset=FALSE)
	{
		if($limit):
			$this->db->limit($limit,$offset);
		endif;
		return $this->db->order_by('konfirmasi_id','desc')
				->get('tb_konfirmasi')->result_array();
	}
	
	private function get_approve($limit=FALSE,$offset=FALSE)
	{
		if($limit):
			$this->db->limit($limit,$offset);
		endif;
		return $this->db->order_by('konfirmasi_id','desc')
				->where('status','approve')
				->get('tb_konfirmasi')->result_array();
	}
	
	private function get_unapprove($limit=FALSE,$offset=FALSE)
	{
		if($limit):
			$this->db->limit($limit,$offset);
		endif;
		return $this->db->order_by('konfirmasi_id','desc')
				->where('status','unapprove')
				->get('tb_konfirmasi')->result_array();
	}
	
}